<?php
    $materialID = $_GET['material'];
    $reserve_date = date("Y-m-d H:i.s",time());
    $expire_date = date("Y-m-d H:i.s",time()+86400);
    echo "$reserve_date";
    echo "$expire_date";

     if(isset($_COOKIE["user"]))
     {


        reserve_material($materialID,$reserve_date,$expire_date);

  }
  else
  {
    echo"<script> alert('Please login.');</script>";
    echo("<script> document.location.href='account.php'</script>");
  }


  
  //function to verify reservation
  function reserve_material ($materialID,$reserve_date,$expire_date)
  {
    echo"pwede 2";
    require('db_connection.php');
    //check for book details
    $query2 = "SELECT * FROM tblcollection WHERE materialID like '$materialID'";
    $result2= mysql_query($query2);
    $today = date("Y-m-d H:i.s",time());
    while ($row = mysql_fetch_array($result2))
    {
      $title = $row['title'];
      $author = $row['author'];
      $libraryNo = $row['libraryNo'];
      $copyNo = $row['copyNo']-1;
      $status = $row['status'];
    }
    
   /*
 if($status == "reserved" && $today == $dateExpire && $today > $dateExpire)
    {
      $query3 = "DELETE FROM tblcirculation WHERE materialID like '$materialID'";
      echo "<script>alert('Deleted');</script>";
    }
    else if ($status == "reserved" && $today < $dateExpire)
    {
      echo "<script>alert('This is reserved.')</script>";
    }
*/
    if($status == "reserved" )
    {
        $query5 = "SELECT * FROM tblcirculation WHERE materialID like '$materialID'";
        $result5 = mysql_query($query5);
        while($row=mysql_fetch_array($result5))
        {
          $dateExpire = $row['dateExpire'];
          $dateReserved = $row['dateReserved'];
        }
      //|| $today > $dateExpire
      if($today == $dateExpire )
      {
             $query4 = "DELETE FROM tblcirculation WHERE materialID like '$materialID'";
             $result4 = mysql_query($query4);
             echo $query3;
             $userID = $_COOKIE['user'];
             confirm_reservation($materialID, $userID, $libraryNo,$reserve_date,$expire_date);
             

        
      }
      
      elseif($today > $dateExpire)
      {
         $query4 = "DELETE FROM tblcirculation WHERE materialID like '$materialID'";
         $result4 = mysql_query($query4);
         confirm_reservation($materialID, $userID, $libraryNo,$reserve_date,$expire_date);
      }
      elseif ($today < $dateExpire)
      {
            echo "<script>alert('This is reserved.')</script>";
            echo "<script>history.back();</script>";
      }
    }
    elseif($status == "available")
    {
        confirm_reservation($materialID, $userID, $libraryNo,$reserve_date,$expire_date);
    }
    else
    {
        echo "<script>alert('This material is unavailable. It may be borrowed or damaged.')</script>";
        echo "<script>history.back();</script>";
    }
    
    //$query3 = "UPDATE tblcirculation SET dateReserved='$reserve_date' WHERE materialID like '$materialID'";
    
    
    
   mysql_close();
  }
  
  function confirm_reservation($materialID, $userID, $libraryNo,$reserve_date,$expire_date)
  {
    echo"pwede3";
    require('db_connection.php');
    $query = "UPDATE tblcollection SET  status='reserved' WHERE materialID like '$materialID'";
    $result = mysql_query($query);
    $userID = $_COOKIE['user'];
    $query3 = "INSERT INTO tblcirculation VALUES($userID, 'reserved','',$libraryNo,'','','$reserve_date','$expire_date', $materialID)";
    $result3 = mysql_query($query3);
    echo "$query3";
    $query2 = "SELECT * FROM tblcollection WHERE materialID like '$materialID'";
    $result2= mysql_query($query2);
    
    if($result && $result3)
        {
            echo"<script> alert('Reservation successful.');</script>";
            echo "<script>history.back();</script>";
           // echo"<a href='index.php'> Continue</a>";
            //echo("<script> document.location.href='index.php'</script>");
        }
        else
            echo"<script> alert('Reservation failed.');</script>";
            echo "<script>history.back();</script>";

  mysql_close();
  }
  

?>
